﻿using CPPEI.Coolzon.Designtime.Metadata;
using CPPEI.Coolzon.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace CPPEI.Coolzon.Designer.Views
{
    public static class DeleteCommand
    {
        public static void Executed(object sender, ExecutedRoutedEventArgs e)
        {
            if (MessageBox.Show("是否确定删除？", "提示", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
            {
                if (UIContext.Current.CurrentSelectedNode.NodeType == NodeType.Folder)
                {
                    if (UIContext.Current.CurrentSelectedNode.Children != null && UIContext.Current.CurrentSelectedNode.Children.Count > 0)
                    {
                        CustomMessageBox.ShowWarning("文件夹不为空");
                    }
                    else
                    {
                        ImmediateDelete();
                    }
                    return;
                }
                if (UIContext.Current.CurrentSelectedNode.NodeType == NodeType.Scene)
                {
                    if (UIContext.Current.CurrentSelectedNode.Children != null && UIContext.Current.CurrentSelectedNode.Children.Count > 0)
                    {
                        CustomMessageBox.ShowWarning("场景下子节点不为空");
                        return;
                    }
                }
                if (UIContext.Current.CurrentSelectedNode.NodeStatus == NodeStatus.Add)
                {
                    ImmediateDelete();
                }
                else if (UIContext.Current.SolutionNode != null && (UIContext.Current.SolutionNode.NodeStatus == NodeStatus.CheckOut || UIContext.Current.SolutionNode.NodeStatus == NodeStatus.Add) && UIContext.Current.CurrentSelectedNode.NodeStatus == NodeStatus.CheckOut)
                {
                    Delete();
                    ProgressbarWindow.Instance.ShowProgress(false);
                }
                else
                {
                    List<SolutionTreeViewNode> nodeList = new List<SolutionTreeViewNode>();
                    nodeList.Add(UIContext.Current.SolutionNode);
                    nodeList.Add(UIContext.Current.CurrentSelectedNode);

                    CheckOutThread.Start(nodeList, ReportProgress, Completed);
                    ProgressbarWindow.Instance.ShowProgress(false);
                }
            }
        }

        private static void ImmediateDelete()
        {
            string workspacePath = UIContext.Current.LocalWorkSpace.LocalPath;
            Guid solutionId = UIContext.Current.CurrentSolution.ID;
            SolutionTreeViewNode tvNode = UIContext.Current.CurrentSelectedNode;
            switch (tvNode.NodeType)
            {
                case NodeType.Scene:
                    UIContext.Current.CurrentSolution.RemoveScene(SolutionHelper.GetScene(UIContext.Current.CurrentSolution, tvNode.ID));
                    break;
                case NodeType.Folder:
                    UIContext.Current.CurrentSolution.RemoveFolder(SolutionHelper.GetFolder(UIContext.Current.CurrentSolution, tvNode.ID));
                    break;
                case NodeType.Resource:
                    UIContext.Current.CurrentSolution.RemoveResource(SolutionHelper.GetResource(UIContext.Current.CurrentSolution, tvNode.ID));
                    break;
                case NodeType.Template:
                    UIContext.Current.CurrentSolution.RemoveTemplate(SolutionHelper.GetTemplate(UIContext.Current.CurrentSolution, tvNode.ParentID, tvNode.ID));
                    LocalStorage.SaveSceneFile(workspacePath, UIContext.Current.CurrentSolution.ID, SolutionHelper.GetScene(UIContext.Current.CurrentSolution, tvNode.ParentID));
                    break;
                case NodeType.StaticDataSource:
                    StaticDataSource sDS = SolutionHelper.GetStaticDataSource(UIContext.Current.CurrentSolution, tvNode.ID);
                    UIContext.Current.CurrentSolution.RemoveStaticDataSource(sDS);
                    break;
            }
            LocalStorage.SaveSolutionFile(workspacePath, UIContext.Current.CurrentSolution);
            VSFileMgr.GoToDelete(workspacePath, solutionId, tvNode.ID);

            SolutionTreeViewNodeHelper.RemoveNodeById(tvNode.ID);
        }

        public static void CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            if (UIContext.Current.CurrentSelectedNode != null && UIContext.Current.CurrentSelectedNode.ID != UIContext.Current.SolutionNode.ID && UIContext.Current.CurrentSelectedNode.NodeStatus != NodeStatus.Delete)
            {
                e.CanExecute = true;
            }
            else
            {
                e.CanExecute = false;
            }
        }
        public static void ReportProgress(Guid id, string name, int current, int total)
        {

        }
        /// <summary>
        /// 签出完成
        /// </summary>
        /// <param name="HasError"></param>
        /// <param name="successed"></param>
        /// <param name="total"></param>
        public static void Completed(bool HasError, int successed, int total)
        {
            if (HasError)
            {
                ProgressbarWindow.Instance.Complete();
                return;
            }
            Delete();
        }

        private static void Delete()
        {
            List<SolutionTreeViewNode> nodeList = new List<SolutionTreeViewNode>();
            nodeList.Add(UIContext.Current.CurrentSelectedNode);
            DeleteThread.Start(nodeList, ReportProgress, DeleteCompleted);
        }
        /// <summary>
        /// 删除完成
        /// </summary>
        /// <param name="HasError"></param>
        /// <param name="successed"></param>
        /// <param name="total"></param>
        private static void DeleteCompleted(bool HasError, int successed, int total)
        {
            ProgressbarWindow.Instance.Complete();
            if (HasError)
            {
                return;
            }
            SolutionTreeViewNodeHelper.RemoveNodeById(UIContext.Current.CurrentSelectedNode.ID);
        }
    }
}
